﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.ajax
{
    public partial class size : System.Web.UI.Page
    {
        Maticsoft222.BLL.sqlexec sql = new Maticsoft222.BLL.sqlexec();
        Maticsoft.BLL.size bll = new Maticsoft.BLL.size();
        Maticsoft.BLL.InventorySize sizebll = new Maticsoft.BLL.InventorySize();
        Maticsoft.BLL.Ordertetisize osziebll = new Maticsoft.BLL.Ordertetisize();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "getparent")
                {
                    Response.Write(JsonConvert.SerializeObject(sql.excodesql("select code,''as name from dbo.size group by code").Select(c => new { parentcode = c.Code }).ToList()));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "tetidelete")
                {
                    //判断是否在订单中被引用
                    List<Maticsoft.Model.Ordertetisize> list = osziebll.GetModelList("tetiid='" + Request["id"] + "'");
                    if (list != null && list.Count > 0)
                        Response.Write(2);
                    else
                        Response.Write((sizebll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                else
                {
                    if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                    {
                        Response.Write((bll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                    }
                    else
                    {
                        Maticsoft.Model.size md = new Maticsoft.Model.size();
                        if (!string.IsNullOrEmpty(Request["id"]))
                        {
                            md = bll.GetModel(Convert.ToInt32(Request["id"]));
                            md.code = Request["code"];
                            md.groups = Request["group"];
                            md.maxpx = Request["maxpx"];
                            md.minpx = Request["minpx"];
                            md.name = Request["name"];
                            md.note = Request["note"];
                            md.type = Request["type"];
                            md.value = Request["value"];
                            row = bll.Update(md) ? 2 : 0;
                        }
                        else
                        {
                            md.code = Request["code"];
                            md.groups = Request["group"];
                            md.maxpx = Request["maxpx"];
                            md.minpx = Request["minpx"];
                            md.name = Request["name"];
                            md.note = Request["note"];
                            md.type = Request["type"];
                            md.value = Request["value"];
                            md.cInvStd = Request["cinvstd"];
                            row = bll.Add(md) > 0 ? 1 : 0;
                        }
                        if (row == 1)
                        {
                            Response.Write("1");
                        }
                        else if (row == 2)
                        {
                            Response.Write("2");
                        }
                        else
                        {
                            Response.Write("0");
                        }
                    }
                }
            }
        }
    }
}